home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl67.lha / tcl6.7 / doc / TraceVar.3 < prev   
Text File  |  1993-01-31  |  14KB  |  341 lines

  1. '\"
  2. '\" Copyright 1989 Regents of the University of California
  3. '\" Permission to use, copy, modify, and distribute this
  4. '\" documentation for any purpose and without fee is hereby
  5. '\" granted, provided that this notice appears in all copies.
  6. '\" The University of California makes no representations about
  7. '\" the suitability of this material for any purpose.  It is
  8. '\" provided "as is" without express or implied warranty.
  9. '\" 
  10. '\" $Header: /user6/ouster/tcl/man/RCS/TraceVar.3,v 1.10 93/01/31 15:35:46 ouster Exp $ SPRITE (Berkeley)
  11. '\" 
  12. .so man.macros
  13. .HS Tcl_TraceVar tcl
  14. .VS
  15. .BS
  16. .SH NAME
  17. Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable
  18. .SH SYNOPSIS
  19. .nf
  20. \fB#include <tcl.h>\fR
  21. .sp
  22. int
  23. \fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  24. .sp
  25. int
  26. \fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  27. .sp
  28. \fBTcl_UnTraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  29. .sp
  30. \fBTcl_UnTraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  31. .sp
  32. ClientData
  33. \fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR
  34. .sp
  35. ClientData
  36. \fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR
  37. .SH ARGUMENTS
  38. .AS Tcl_VarTraceProc prevClientData
  39. .AP Tcl_Interp *interp in
  40. Interpreter containing variable.
  41. .AP char *varName in
  42. Name of variable.  May refer to a scalar variable, to
  43. an array variable with no index, or to an array variable
  44. with a parenthesized index.
  45. .AP int flags in
  46. OR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, and
  47. TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY.  Not all flags are used by all
  48. procedures.  See below for more information.
  49. .AP Tcl_VarTraceProc *proc in
  50. Procedure to invoke whenever one of the traced operations occurs.
  51. .AP ClientData clientData in
  52. Arbitrary one-word value to pass to \fIproc\fR.
  53. .AP char *name1 in
  54. Name of scalar or array variable (without array index).
  55. .AP char *name2 in
  56. For a trace on an element of an array, gives the index of the
  57. element.  For traces on scalar variables or on whole arrays,
  58. is NULL.
  59. .AP ClientData prevClientData in
  60. If non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or
  61. \fBTcl_VarTraceInfo2\fR, so this call will return information about
  62. next trace.  If NULL, this call will return information about first
  63. trace.
  64. .BE
  65.  
  66. .SH DESCRIPTION
  67. .PP
  68. \fBTcl_TraceVar\fR allows a C procedure to monitor and control
  69. access to a Tcl variable, so that the C procedure is invoked
  70. whenever the variable is read or written or unset.
  71. If the trace is created successfully then \fBTcl_TraceVar\fR returns
  72. TCL_OK.  If an error occurred (e.g. \fIvarName\fR specifies an element
  73. of an array, but the actual variable isn't an array) then TCL_ERROR
  74. is returned and an error message is left in \fIinterp->result\fR.
  75. .PP
  76. The \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when the
  77. trace procedure is to be invoked and provides information
  78. for setting up the trace.  It consists of an OR-ed combination
  79. of any of the following values:
  80. .TP
  81. \fBTCL_GLOBAL_ONLY\fR
  82. Normally, the variable will be looked up at the current level of
  83. procedure call;  if this bit is set then the variable will be looked
  84. up at global level, ignoring any active procedures.
  85. .TP
  86. \fBTCL_TRACE_READS\fR
  87. Invoke \fIproc\fR whenever an attempt is made to read the variable.
  88. .TP
  89. \fBTCL_TRACE_WRITES\fR
  90. Invoke \fIproc\fR whenever an attempt is made to modify the variable.
  91. .TP
  92. \fBTCL_TRACE_UNSETS\fR
  93. Invoke \fIproc\fR whenever the variable is unset.
  94. A variable may be unset either explicitly by an \fBunset\fR command,
  95. or implicitly when a procedure returns (its local variables are
  96. automatically unset) or when the interpreter is deleted (all
  97. variables are automatically unset).
  98. .PP
  99. Whenever one of the specified operations occurs on the variable,
  100. \fIproc\fR will be invoked.
  101. It should have arguments and result that match the type
  102. \fBTcl_VarTraceProc\fR:
  103. .nf
  104. .RS
  105. typedef char *Tcl_VarTraceProc(
  106. .RS
  107. ClientData \fIclientData\fR,
  108. Tcl_Interp *\fIinterp\fR,
  109. char *\fIname1\fR,
  110. char *\fIname2\fR,
  111. int \fIflags\fR);
  112. .RE
  113. .RE
  114. .fi
  115. The \fIclientData\fP and \fIinterp\fP parameters will
  116. have the same values as those passed to \fBTcl_TraceVar\fR when the
  117. trace was created.
  118. \fIClientData\fR typically points to an application-specific
  119. data structure that describes what to do when \fIproc\fR
  120. is invoked.
  121. \fIName1\fR and \fIname2\fR give the name of the traced variable
  122. in the normal two-part form (see the description of \fBTcl_TraceVar2\fR
  123. below for details).
  124. \fIFlags\fR is an OR-ed combination of bits providing several
  125. pieces of information.
  126. One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, or TCL_TRACE_UNSETS
  127. will be set in \fIflags\fR to indicate which operation is being performed
  128. on the variable.
  129. The bit TCL_GLOBAL_ONLY will be set whenever the variable being
  130. accessed is a global one not accessible from the current level of
  131. procedure call:  the trace procedure will need to pass this flag
  132. back to variable-related procedures like \fBTcl_GetVar\fR if it
  133. attempts to access the variable.
  134. The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace is
  135. about to be destroyed;  this information may be useful to \fIproc\fR
  136. so that it can clean up its own internal data structures (see
  137. the section TCL_TRACE_DESTROYED below for more details).
  138. Lastly, the bit TCL_INTERP_DESTROYED will be set if the entire
  139. interpreter is being destroyed.
  140. When this bit is set, \fIproc\fR must be especially careful in
  141. the things it does (see the section TCL_INTERP_DESTROYED below).
  142. The trace procedure's return value should normally be NULL;  see
  143. ERROR RETURNS below for information on other possibilities.
  144. .PP
  145. \fBTcl_UntraceVar\fR may be used to remove a trace.
  146. If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fR
  147. has a trace set with \fIflags\fR, \fIproc\fR, and
  148. \fIclientData\fR, then the corresponding trace is removed.
  149. If no such trace exists, then the call to \fBTcl_UntraceVar\fR
  150. has no effect.
  151. The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVars\fR.
  152. .PP
  153. \fBTcl_VarTraceInfo\fR may be used to retrieve information about
  154. traces set on a given variable.
  155. The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fR
  156. associated with a particular trace.
  157. The trace must be on the variable specified by the \fIinterp\fR,
  158. \fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLY
  159. bit from \fIflags\fR is used;  other bits are ignored) and its trace procedure
  160. must the same as the \fIproc\fR argument.
  161. If the \fIprevClientData\fR argument is NULL then the return
  162. value corresponds to the first (most recently created) matching
  163. trace, or NULL if there are no matching traces.
  164. If the \fIprevClientData\fR argument isn't NULL, then it should
  165. be the return value from a previous call to \fBTcl_VarTraceInfo\fR.
  166. In this case, the new return value will correspond to the next
  167. matching trace after the one whose \fIclientData\fR matches
  168. \fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fR
  169. or if there are no more matching traces after it.
  170. This mechanism makes it possible to step through all of the
  171. traces for a given variable that have the same \fIproc\fR.
  172.  
  173. .SH "TWO-PART NAMES"
  174. .PP
  175. The procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and
  176. \fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR,
  177. \fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively,
  178. except that the name of the variable has already been
  179. separated by the caller into two parts.
  180. \fIName1\fR gives the name of a scalar variable or array,
  181. and \fIname2\fR gives the name of an element within an
  182. array.
  183. If \fIname2\fR is NULL it means that either the variable is
  184. a scalar or the trace is to be set on the entire array rather
  185. than an individual element (see WHOLE-ARRAY TRACES below for
  186. more information).
  187.  
  188. .SH "ACCESSING VARIABLES DURING TRACES"
  189. .PP
  190. During read and write traces, the
  191. trace procedure can read or write the value of the traced
  192. variable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, and
  193. other procedures.
  194. While \fIproc\fR is executing, traces are temporarily disabled
  195. for the variable, so that calls to \fBTcl_GetVar2\fR and
  196. \fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace procedures
  197. to be invoked again.
  198. Disabling only occurs for the variable whose trace procedure
  199. is active;  accesses to other variables will still be traced.
  200. .PP
  201. During unset traces the variable has already been completely
  202. expunged.
  203. It is possible for the trace procedure to read or write the
  204. variable, but this will be a new version of the variable.
  205. Traces are not disabled during unset traces as they are for
  206. read and write traces, but existing traces have been removed
  207. from the variable before any trace procedures are invoked.
  208. If new traces are set by unset trace procedures, these traces
  209. will be invoked on accesses to the variable by the trace
  210. procedures.
  211.  
  212. .SH "CALLBACK TIMING"
  213. .PP
  214. When read tracing has been specified for a variable, the trace
  215. procedure will be invoked whenever the variable's value is
  216. read.  This includes \fBset\fR Tcl commands, \fB$\fR-notation
  217. in Tcl commands, and invocations of the \fBTcl_GetVar\fR
  218. and \fBTcl_GetVar2\fR procedures.
  219. \fIProc\fR is invoked just before the variable's value is
  220. returned.
  221. It may modify the value of the variable to affect what
  222. is returned by the traced access.
  223. .PP
  224. When write tracing has been specified for a variable, the
  225. trace procedure will be invoked whenever the variable's value
  226. is modified.  This includes \fBset\fR commands\fR,
  227. commands that modify variables as side effects (such as
  228. \fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fR
  229. and \fBTcl_SetVar2\fR procedures).
  230. \fIProc\fR will be invoked after the variable's value has been
  231. modified, but before the new value of the variable has been
  232. returned.
  233. It may modify the value of the variable to override the change
  234. and to determine the value actually returned by the traced
  235. access.
  236. .PP
  237. When unset tracing has been specified, the trace procedure
  238. will be invoked whenever the variable is destroyed.
  239. The traces will be called after the variable has been
  240. completely unset.
  241.  
  242. .SH "WHOLE-ARRAY TRACES"
  243. .PP
  244. If a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifies
  245. the name of an array variable without an index into the array,
  246. then the trace will be set on the array as a whole.
  247. This means that \fIproc\fR will be invoked whenever any
  248. element of the array is accessed in the ways specified by
  249. \fIflags\fR.
  250. When an array is unset, a whole-array trace will be invoked
  251. just once, with \fIname1\fR equal to the name of the array
  252. and \fIname2\fR NULL;  it will not be invoked once for each
  253. element.
  254.  
  255. .SH "MULTIPLE TRACES"
  256. .PP
  257. It is possible for multiple traces to exist on the same variable.
  258. When this happens, all of the trace procedures will be invoked on each
  259. access, in order from most-recently-created to least-recently-created.
  260. When there exist whole-array traces for an array as well as
  261. traces on individual elements, the whole-array traces are invoked
  262. before the individual-element traces.
  263.  
  264. .SH "ERROR RETURNS"
  265. .PP
  266. Under normal conditions trace procedures should return NULL, indicating
  267. successful completion.
  268. If \fIproc\fR returns a non-NULL value it signifies that an
  269. error occurred.
  270. The return value must be a pointer to a static character string
  271. containing an error message.
  272. If a trace procedure returns an error, no further traces are
  273. invoked for the access and the traced access aborts with the
  274. given message.
  275. Trace procedures can use this facility to make variables
  276. read-only, for example (but note that the value of the variable
  277. will already have been modified before the trace procedure is
  278. called, so the trace procedure will have to restore the correct
  279. value).
  280. .PP
  281. The return value from \fIproc\fR is only used during read and
  282. write tracing.
  283. During unset traces, the return value is ignored and all relevant
  284. trace procedures will always be invoked.
  285.  
  286. .SH "RESTRICTIONS"
  287. .PP
  288. It is not legal to delete a variable while a trace procedure
  289. is active for the variable.
  290. .PP
  291. .VS
  292. Also, a trace procedure can be called at any time, even when there
  293. is a partically-formed result in the interpreter's result area.  If
  294. the trace procedure does anything that could damage this result (such
  295. as calling \fBTcl_Eval\fR) then it must save the original values of
  296. the interpreter's \fBresult\fR and \fBfreeProc\fR fields and restore
  297. them before it returns.
  298. .VE
  299.  
  300. .SH "UNDEFINED VARIABLES"
  301. .PP
  302. It is legal to set a trace on an undefined variable.
  303. The variable will still appear to be undefined until the
  304. first time its value is set.
  305. If an undefined variable is traced and then unset, the unset will fail
  306. with an error (``no such variable''), but the trace
  307. procedure will still be invoked.
  308.  
  309. .SH "TCL_TRACE_DELETED FLAG"
  310. .PP
  311. In an unset callback to \fIproc\fR, the TCL_TRACE_DELETED bit
  312. is set in \fIflags\fR if the trace is being removed as part
  313. of the deletion.
  314. Traces on a variable are always removed whenever the variable
  315. is deleted;  the only time TCL_TRACE_DELETED isn't set is for
  316. a whole-array trace invoked when only a single element of an
  317. array is unset.
  318.  
  319. .SH "TCL_INTERP_DESTROYED"
  320. .PP
  321. When an interpreter is destroyed, unset traces are called for
  322. all of its variables.
  323. The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fR
  324. argument passed to the trace procedures.
  325. Trace procedures must be extremely careful in what they do if
  326. the TCL_INTERP_DESTROYED bit is set.
  327. It is not safe for the procedures to invoke any Tcl procedures
  328. on the interpreter, since its state is partially deleted.
  329. All that trace procedures should do under these circumstances is
  330. to clean up and free their own internal data structures.
  331.  
  332. .SH BUGS
  333. .PP
  334. Tcl doesn't do any error checking to prevent trace procedures
  335. from misusing the interpreter during traces with TCL_INTERP_DESTROYED
  336. set.
  337.  
  338. .SH KEYWORDS
  339. clientData, trace, variable
  340. .VE
  341.